/*
 * AS3 MultiTouch Client
 *
 * TouchBehaviour - Superclass for all behaviour classes.
 * A TouchBehaviour can handle arbitrary touch events dispatched by the manager
 * and are used to affect the object position, rotation, scale, etc. when touched.
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
/**
 * TouchBehaviour - Superclass for all behaviour classes.
 * A TouchBehaviour can handle arbitrary touch events dispatched by the manager
 * and are used to affect the object position, rotation, scale, etc. when touched.
 */
package de.artifacts.multitouch.behaviours
{
	import de.artifacts.multitouch.Cursor;
	import de.artifacts.multitouch.TouchEvent;
	import de.artifacts.multitouch.TouchManager;
	import de.artifacts.multitouch.Touchable;
	
	import flash.utils.getTimer;
	
	public class TouchBehaviour
	{
		public var touchManager : TouchManager;
		public var touchable : Touchable;
		
		public function TouchBehaviour()
		{
		}		
		
		public function touchedHandler(event : TouchEvent) : void
		{			
			if (event.cursor==null) return;
			if (event.cursor.touchables.indexOf(touchable)>-1)			
			{
				var now : int = getTimer();
				var timeBetweenTaps : int = now - touchable.lastTouchTime;
				touchable.doubleTap = (timeBetweenTaps > Touchable.DOUBLE_TAP_INTERVAL_MIN && 
									   timeBetweenTaps < Touchable.DOUBLE_TAP_INTERVAL_MAX);								 		
				touchable.lastTouchTime = now;
			}
		}
						
		public function releasedHandler(event : TouchEvent) : void
		{
			var now : int = getTimer();
			var tapDuration : int = now - touchable.lastTouchTime;
			event.cursor.shouldSurpressInterpretationAsTap =  !(tapDuration > Touchable.SINGLE_TAP_DURATION_MIN && 
			  													tapDuration < Touchable.SINGLE_TAP_DURATION_MAX);
		}
		
		public function repositionedHandler(event : TouchEvent) : void
		{
			touchable.lastTouchTime = getTimer();
		}
		
		public function getFirstCursorThatTouchesMe() : Cursor
		{
			var cursor : Cursor;
			for each (cursor in touchManager.cursors) {				
				if (cursor.touches(touchable)) return cursor;
			}
			return null;
		}
		
	}	
}